<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/7/29
 * Time: 23:07
 */

namespace app\admin\controller;

use app\admin\model\Attendance as AttendanceModel;
use app\admin\model\AttendanceDetail;
use app\admin\model\AttendanceOvertime;
use app\admin\model\WechatDepartment;

class Attendance extends Admin
{
    /**
     * 上班打卡导出
     */
    public function work() {
        $attendanceDetailModel = new AttendanceDetail();
       // 时间
        $time = input('time',date("Y-m-d",time()));
        if(empty($time)) {  $time = date("Y-m-d",time()); }
        $list = $attendanceDetailModel->getList($time);
        $this->assign('list',$list);
        $this->assign('time',$time);
        return $this->fetch();
    }

    /**
     * 上班导出
     */
    public function export1() {
        //引入Excel类
        $objPHPExcel = new \PHPExcel();
        // 设置excel文档的属性
        $objPHPExcel->getProperties()->setCreator("cyf")
            ->setLastModifiedBy("cyf Test")
            ->setTitle("order")
            ->setSubject("Test1")
            ->setDescription("Test2")
            ->setKeywords("Test3")
            ->setCategory("Test result file");
        //设置excel工作表名及文件名
        $title = '考勤上班打卡记录表';
        $excel_filename = '考勤上班打卡记录表_'.date('YmdHis');
        // 操作第一个工作表
        $objPHPExcel->setActiveSheetIndex(0);
        //第一行设置内容
        $objPHPExcel->getActiveSheet()->setCellValue('A1',$excel_filename);
        //合并
        $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置单元格内容加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //设置单元格内容水平居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置excel的表头
        $sheet_title = array('部门','用户','考勤状态','考勤时间','上报人','上报时间');
        // 设置第一行和第一行的行高
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);
        $letter = array('A','B','C','D','E','F');
        //设置单元格
        $objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        //首先是赋值表头
        for ($k=0;$k<6;$k++) {
            $objPHPExcel->getActiveSheet()->setCellValue($letter[$k].'2',$sheet_title[$k]);
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getFont()->setSize(10)->setBold(true);
            //设置单元格内容水平居中
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置每一列的宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k])->setWidth(18);
            $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(30);
        }

        $attendanceDetailModel = new AttendanceDetail();
        // 时间
        $time = input('time',date("Y-m-d",time()));
        if(empty($time)) {  $time = date("Y-m-d",time()); }
        $list = $attendanceDetailModel->getList($time);
        $count = count($list);
        //开始赋值
        for ($i=0;$i<$count;$i++) {
            //先确定行
            $row = $i+3;//再确定列，最顶部占一行，表头占用一行，所以加3
            $temp = $list[$i];
            for ($j = 0; $j<6; $j++) {
                //开始为每个单元格赋值
                switch ($j) {
                    case 0 :
                        //部门名
                        $cellvalue = $temp['departmentname'];
                        break;
                    case 1 :
                        //用户名
                        $cellvalue = $temp['username'];
                        break;
                    case 2 :
                        //状态
                        $cellvalue = $temp['status_text'];
                        break;
                    case 3:
                        $cellvalue = $temp['attendance_time'];
                        break;
                    case 4 :
                        //上报人
                        $cellvalue = $temp['report_user'];
                        break;
                    case 5 :
                        //上报时间
                        $cellvalue = $temp['report_time'];
                        break;
                }
                //赋值
                $objPHPExcel->getActiveSheet()->setCellValue($letter[$j].$row, $cellvalue);
                //设置字体大小
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getFont()->setSize(10);
                //设置单元格内容水平居中
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
            // 设置行高
            $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(21);
        }
        unset($list);

        //赋值结束，开始输出
        $objPHPExcel->getActiveSheet()->setTitle($title);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$excel_filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //生成xlsx文件并存入当前文件目录
        function saveExcelToLocalFile($objWriter,$excel_filename)
        {
            // make sure you have permission to write to directory
            $filePath = './uploads/excel/' . $excel_filename . '.xls';
            $objWriter->save($filePath);
            return $filePath;
        }
        $response = [
            'code' => 0,
            'url' => saveExcelToLocalFile($objWriter,$excel_filename),
        ];
        return json_encode($response);
//        dump($objWriter);
//        $objWriter->save('php://output');
    }

    /**
     * 加班记录导出
     */
    public function overtime() {
        $attendanceOvertimeModel = new AttendanceOvertime();
        $map = array(
            'status' => array('egt',0),
        );
        $search = input('search');
        //搜索
        if ($search != '') {
            $map['username'] = ['like', '%' . $search . '%'];
        }
        //时间
        $start_time = input('start_time');
        $end_time = input('end_time');
        if($start_time != '' && $end_time != '') {
            $map['start_time'] = ['between',[strtotime($start_time." 00:00:00"),strtotime($end_time." 23:59:59")]];
        }else if($start_time != '') {
            $map['start_time'] = ['egt',strtotime($start_time." 00:00:00")];
        }else if($end_time != '') {
            $map['start_time'] = ['elt',strtotime($end_time." 23:59:59")];
        }
        $list = $attendanceOvertimeModel::where($map)->order('start_time desc')->paginate(10);
        foreach ($list as $key => $value) {
            $department = WechatDepartment::where('id',$value['departmentid'])->find();
            $value['departmentname'] = $department['name'];
            $data = [1 => "加班", 2 => "外勤"];
            $value['type_text'] = $data[$value['type']];
        }
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 加班导出
     */
    public function export2() {
        //引入Excel类
        $objPHPExcel = new \PHPExcel();
        // 设置excel文档的属性
        $objPHPExcel->getProperties()->setCreator("cyf")
            ->setLastModifiedBy("cyf Test")
            ->setTitle("order")
            ->setSubject("Test1")
            ->setDescription("Test2")
            ->setKeywords("Test3")
            ->setCategory("Test result file");
        //设置excel工作表名及文件名
        $title = '周末加班打卡记录表';
        $excel_filename = '周末加班打卡记录表'.date('YmdHis');
        // 操作第一个工作表
        $objPHPExcel->setActiveSheetIndex(0);
        //第一行设置内容
        $objPHPExcel->getActiveSheet()->setCellValue('A1',$excel_filename);
        //合并
        $objPHPExcel->getActiveSheet()->mergeCells('A1:H1');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置单元格内容加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //设置单元格内容水平居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置excel的表头
        $sheet_title = array('部门名','用户名','签到类型','签到时间','签到地点','签退时间','签退地点','状态');
        // 设置第一行和第一行的行高
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);
        $letter = array('A','B','C','D','E','F','G','H');
        //设置单元格
        $objPHPExcel->getActiveSheet()->getStyle('A2:H1')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        //首先是赋值表头
        for ($k=0;$k<8;$k++) {
            $objPHPExcel->getActiveSheet()->setCellValue($letter[$k].'2',$sheet_title[$k]);
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getFont()->setSize(10)->setBold(true);
            //设置单元格内容水平居中
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置每一列的宽度
            if($k == 4 || $k == 6) {
                $objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k])->setWidth(60);
                $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(60);
            }else {
                $objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k])->setWidth(18);
                $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(30);
            }
        }
        //获取数据
        $attendanceOvertimeModel = new AttendanceOvertime();
        $map = array(
            'status' => array('egt',0),
        );
        $search = input('search');
        //搜索
        if ($search != '') {
            $map['username'] = ['like', '%' . $search . '%'];
        }
        //时间
        $start_time = input('start_time');
        $end_time = input('end_time');
        if($start_time != '' && $end_time != '') {
            $map['start_time'] = ['between',[strtotime($start_time." 00:00:00"),strtotime($end_time." 23:59:59")]];
        }else if($start_time != '') {
            $map['start_time'] = ['egt',strtotime($start_time." 00:00:00")];
        }else if($end_time != '') {
            $map['start_time'] = ['elt',strtotime($end_time." 23:59:59")];
        }
        $list = $attendanceOvertimeModel::where($map)->order('start_time desc')->select();
        $count = count($list);
        foreach ($list as $key => $value) {
            $department = WechatDepartment::where('id',$value['departmentid'])->find();
            $value['departmentname'] = $department['name'];
            $data = [1 => "加班", 2 => "外勤"];
            $value['type_text'] = $data[$value['type']];
            $data2 = [0 => "缺卡", 1 => "正常"];
            $value['status_text'] = $data2[$value['status']];
        }
        //开始赋值
        for ($i=0;$i<$count;$i++) {
            //先确定行
            $row = $i+3;//再确定列，最顶部占一行，表头占用一行，所以加3
            $temp = $list[$i];
            for ($j = 0; $j<8; $j++) {
                //开始为每个单元格赋值
                switch ($j) {
                    case 0 :
                        //部门名
                        $cellvalue = $temp['departmentname'];
                        break;
                    case 1 :
                        //用户名
                        $cellvalue = $temp['username'];
                        break;
                    case 2 :
                        //签到类型
                        $cellvalue = $temp['type_text'];
                        break;
                    case 3 :
                        //签到时间
                        $cellvalue = date('Y-m-d H:i',$temp['start_time']);
                        break;
                    case 4 :
                        //签到地点
                        $cellvalue = $temp['start_address'];
                        break;
                    case 5 :
                        //签退时间
                        $cellvalue = $temp['end_time'] ? date('Y-m-d H:i',$temp['end_time']) : "——";
                        break;
                    case 6 :
                        //签退地点
                        $cellvalue = $temp['end_address'] ? $temp['end_address'] : "——";
                        break;
                    case 7 :
                        //状态
                        $cellvalue = $temp['status_text'];
                        break;
                }
                //赋值
                $objPHPExcel->getActiveSheet()->setCellValue($letter[$j].$row, $cellvalue);
                //设置字体大小
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getFont()->setSize(10);
                //设置单元格内容水平居中
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
            // 设置行高
            $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(21);
        }
        unset($list);

        //赋值结束，开始输出
        $objPHPExcel->getActiveSheet()->setTitle($title);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$excel_filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //生成xlsx文件并存入当前文件目录
        function saveExcelToLocalFile($objWriter,$excel_filename)
        {
            // make sure you have permission to write to directory
            $filePath = './uploads/excel/' . $excel_filename . '.xls';
            $objWriter->save($filePath);
            return $filePath;
        }
        $response = [
            'code' => 0,
            'url' => saveExcelToLocalFile($objWriter,$excel_filename),
        ];
        return json_encode($response);
//        $objWriter->save('php://output');
    }
}